/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkFolios;

namespace Whitemice.Consonance
{
  public partial class AppointmentWindow : EntityWindow, 
                                           IEntityView, 
                                           IZOGIWidget
  {
    protected Whitemice.ZOGI.Appointment                appointment;
    protected Whitemice.ZOGI.GtkFolios.AppointmentFolio folio;
    protected LabelEntityStore                          teams;
    
    public AppointmentWindow(Whitemice.ZOGI.Appointment _appointment) : base(Gtk.WindowType.Toplevel) 
    {
      CellRendererText  text;
    
      this.Build();

      callback = new MultiResultCallback(this.ReceiveDataCallback);
                        
			folio = new AppointmentFolio();
			//folio.NoteTab.OnEntityPresent += new EntityEventHandler(PresentEntity);
			//folio.LinksTab.OnEntityPresent += new EntityEventHandler(PresentEntity);
			folioArea.Child = folio;
			
			teams = new LabelEntityStore();
			folio.DetailForm.Reader.Model = teams;
			//Registry.Server.Search("Team", "all", callback, "AppointmentReader:Teams");
			text = new CellRendererText();
      folio.DetailForm.Reader.PackStart(text, false);
      folio.DetailForm.Reader.AddAttribute(text, "text", 0);
			Fill(_appointment);
			ShowAll();
    } // end ctor

    public override Entity Current { get { return appointment; } }
    
    protected override bool RunCallbackCommand(string command, RPCEventArgs args)
    {
      // RunCallbackCommand returns true if the method should continue, a false
      // response means that the command was processed.
      if (base.RunCallbackCommand(command, args))
      {
        this.ZLOG("Unhandled command {0} passed to RunCallbackCommand.", command);
        // we would put custom command processing for appointment windows
        // specific callbacks here.
        return false;
      } else return false;
    } // end RunCallbackCommand
      
    protected override void Fill(Entity entity)
    {
      this.EntityNameCheck(entity, typeof(Whitemice.ZOGI.Appointment));
      appointment = (entity as Whitemice.ZOGI.Appointment);
      FreezeToDepth(1);
      Registry.RegisterForNotification(appointment, this);
      folio.Fill(Current);
      SetTitle();
      if (appointment.CanModify)
      {
      } else
        {
        }
    }
		
		protected override bool Take()
		{
			return folio.Take(appointment);
		} // end Take
		
		protected void SetTitle()
		{
			if (appointment.ObjectId < 0)
			{
				Title = "New Appointment";
			} else
				{
					Title = "Appointment#" + 
						appointment.ObjectId.ToString() + ":" + 
						appointment.Title;
				}
		} // end SetTitle 
		
    public override void ProcessEvent(Whitemice.ZOGI.ServerEvent _event)
    {
      switch(_event.Event)
      {
        case ServerEventType.Update:
          Fill();
          break;
        case ServerEventType.SubordinateChange:
          break;
      } /* end event-switch */
    } /* end ReceiveEvent */

    protected virtual void OnRemoveClicked (object sender, System.EventArgs e)
    {
      Remove(appointment);
    } /* end OnRemoveClicked */

    protected virtual void OnApplyClicked (object sender, System.EventArgs e)
    {
			Save(appointment, false);
    } /* end OnApplyClicked */

    protected virtual void OnCloseButtonClicked (object sender, System.EventArgs e)
    {
      Unregister(appointment, true);
    } /* end OnCloseButtonClicked */

    protected virtual void OnOkClicked (object sender, System.EventArgs e)
    {
		  Save(appointment, true);
    } /* end OnOkClicked */

    protected virtual void OnDeleteEvent (object o, Gtk.DeleteEventArgs args)
    {
      Unregister(appointment, true);
    } // end OnDeleteEvent
  } // end class AppointmentWindow 
} // end namespace
